package CAT200;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.sql.*;

/**
 *
 * @author Wai Hong
 */
public class Edit_Item extends javax.swing.JFrame {

	int sizeCount, colorCount;
	String colorCombo, sizeCombo;
	
    /** Creates new form Edit_Item2 */
    public Edit_Item() {
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        sizeCount = 0;
        colorCount = 0;
        Calendar cal = new GregorianCalendar();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        dateLabel.setText(year+"-"+(month+1)+"-"+day);
    }

    public Edit_Item(String id) {
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        sizeCount = 0;
        colorCount = 0;
        Calendar cal = new GregorianCalendar();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        dateLabel.setText(year+"-"+(month+1)+"-"+day);
        codeLabel.setText(id);
        getData(id);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        sizeBox = new javax.swing.JComboBox();
        colorBox = new javax.swing.JComboBox();
        quantityLabel = new javax.swing.JTextField();
        priceLabel = new javax.swing.JTextField();
        descriptionLabel = new javax.swing.JTextField();
        pictureTextfield = new javax.swing.JTextField();
        BrowseButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        SaveButton = new javax.swing.JButton();
        codeLabel = new javax.swing.JLabel();
        dateLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Edit Item", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 0, 12), new java.awt.Color(53, 57, 58))); // NOI18N

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel2.setText("CODE :");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel3.setText("Description :");

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); 
        jLabel4.setText("Quantity :");

        jLabel5.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel5.setText("Price :");

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel6.setText("Date :");

        jLabel7.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel7.setText("Size :");

        jLabel8.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel8.setText("Colour :");

        jLabel9.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        jLabel9.setText("Picture :");

        sizeBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose One", "S", "M", "L", "XL", "XXL" }));
        sizeBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            //    sizeBoxActionPerformed(evt);
            }
        });

        colorBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose One", "BLACK", "WHITE", "BLUE", "RED", "YELLOW" }));
        colorBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
    //            colorBoxActionPerformed(evt);
            }
        });

        BrowseButton.setText("Browse");
        BrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowseButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        SaveButton.setText("Save Changes");
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	SaveButtonActionPerformed(evt);
            	
            }
        });

        codeLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14));
        codeLabel.setText("  ");
        codeLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        dateLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); // NOI18N
        dateLabel.setText("  ");
        dateLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        
        descriptionLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); // NOI18N

        quantityLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); // NOI18N

        priceLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 14)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel9)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(pictureTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BrowseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE))
                            .addComponent(descriptionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(dateLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(priceLabel, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(quantityLabel, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(colorBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(sizeBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(codeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23)
                        .addComponent(SaveButton)
                        .addGap(27, 27, 27))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(codeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(descriptionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(quantityLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(priceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(dateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(sizeBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(colorBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(pictureTextfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BrowseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SaveButton)
                    .addComponent(CancelButton))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 423, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    
    private void BrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	
    	String id = codeLabel.getText();
        String P_Desciption = descriptionLabel.getText();
        String P_Quantity = quantityLabel.getText();
        String P_Price = priceLabel.getText();
        String P_Date = dateLabel.getText();
        String P_Location = pictureTextfield.getText();
        String P_Size = sizeCombo;
        String P_Colour = colorCombo;
        Object a =colorBox.getSelectedItem();
        String b = a.toString();
        Object c =sizeBox.getSelectedItem();
        String d = c.toString();
        
    	try
    	{
    	DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/CAT200-DB", "user", "123");
        
        Statement stmt = con.createStatement();

        String Query = "UPDATE GALLERY SET P_DESCRIPTION = '"+P_Desciption+"',P_QUANTITY = "+P_Quantity+", P_PRICE = "+P_Price+", P_INDATE= '"+P_Date+"',P_COLOR = '"+b+"',P_SIZE = '"+d+"',P_LOCATION = '"+P_Location+"' WHERE PRODUCT_CODE = '"+id+"'";

        stmt.executeUpdate(Query);

        con.close();
        stmt.close();      

        
    	}
    	catch(Exception e)
    	{  
    	System.out.println("Insert data error: "+e);	
    	}
    	this.dispose();
    	new AdminMenu().setVisible(true);
    }

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	this.setVisible(false);
    	this.dispose();
    	new AdminMenu().setVisible(true);
    }
    

    
    private void getData(String id)
    {
    	try
    	{
    	DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/CAT200-DB", "user", "123");
        
        Statement stmt = con.createStatement();
        ResultSet result = stmt.executeQuery("select P_DESCRIPTION, P_QUANTITY, P_PRICE, P_INDATE,P_SIZE,P_COLOR,P_LOCATION from GALLERY WHERE PRODUCT_CODE = '"+id+"'");

        while(result.next())
        {
            
        	descriptionLabel.setText(result.getString("P_DESCRIPTION"));
        	quantityLabel.setText(result.getString("P_QUANTITY"));
        	priceLabel.setText(result.getString("P_PRICE"));
            pictureTextfield.setText(result.getString("P_LOCATION"));
            String sizeCombo = result.getString("P_SIZE");
            
            if (sizeCombo == "S"){
            	
            	sizeBox.setSelectedIndex(1);
            }
            else if (sizeCombo == "M"){
            	sizeBox.setSelectedIndex(2);
            }
            else if (sizeCombo == "L"){
            	sizeBox.setSelectedIndex(3);
            }
            else if (sizeCombo == "XL"){
            	sizeBox.setSelectedIndex(4);
            }
            else if (sizeCombo == "XXL"){
            	sizeBox.setSelectedIndex(5);
            }       
            
            String colorCombo = result.getString("P_COLOR");
            
            if (colorCombo == "BLACK"){
            	colorBox.setSelectedIndex(1);
            }
            else if (colorCombo == "WHITE"){
            	colorBox.setSelectedIndex(2);
            }
            else if (colorCombo == "BLUE"){
            	colorBox.setSelectedIndex(3);
            }
            else if (colorCombo == "RED"){
            	colorBox.setSelectedIndex(4);
            }
            else if (colorCombo == "YELLOW"){
            	colorBox.setSelectedIndex(5);
            }         
        }

        con.close();
        stmt.close();
    	}    	catch(Exception e)
    	{  
        	System.out.println("Get data error: "+e);	
        	}
    }
    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Edit_Item().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton BrowseButton;
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton SaveButton;
    private javax.swing.JLabel codeLabel;
    private javax.swing.JComboBox colorBox;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JTextField descriptionLabel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField pictureTextfield;
    private javax.swing.JTextField priceLabel;
    private javax.swing.JTextField quantityLabel;
    private javax.swing.JComboBox sizeBox;
    // End of variables declaration

}
